
local locplayer = ac.player.self
local list1 = {}
local list2 = {}
for a,temp in ipairs(load_excel('商店设置.xlsx','杀敌第一页')) do
    local name = temp.name
    temp.food = 100
    temp.is_shop = true
    temp.empty_item = true
    temp.state = text_unpack_attrs(temp.base_tip)
    temp.tip = [[|cffff9900'base_tip'|r|n|n|n|cff999999成功概率：|r|cffff0000'odds'%|r|n|cff999999每次失败提升5%成功概率|r]]
    list1[a] = name
    ac.skill(name,temp)
end

for a,temp in ipairs(load_excel('商店设置.xlsx','杀敌第二页')) do
    local name = temp.name
    temp.is_shop = true
    temp.empty_item = true
    temp.show_num = 0
    temp.index = a
    temp.state = text_unpack_attrs(temp.base_tip)
    temp.tip = [[|cffff9900'base_tip'|r|n|n|n|cff999999兑换上限：|r|cffff0000'show_num'/'max'|r|n|cff999999右键自动十连|r]]
    list2[a] = name
    ac.skill(name,temp)
end


local mt = ac.skill['杀敌兑换']

function mt:on_add()
    local unit = self.owner
    local player = unit.owner
    self:remove()
    shop_add_panel(unit)
    if ac.init_game==nil then
        return
    end
    player:event '玩家-显示商店'(function(trg,_,name)
        if name=='杀敌兑换' then
            trg:remove()
            if ac.enemy.hard<3 then
                player:set_camera(point,0.5)
            end
            player:event '玩家-选择单位'(function(trg,_,select_unit)
                if select_unit==unit then
                    trg:remove()
                end
            end)
        end
    end)
    for _,name in ipairs(list1) do
        unit:add_skill(name,'英雄')
    end
    
end

for _,name in ipairs(list1) do
    local mt = ac.skill[name]

    function mt:on_add()
        local unit = self.owner
        local player = unit.owner
        self:gc(ac.loop(200,function()
            if player:get('杀敌')>=self:get_food() then
                if (unit.show_effect==nil or unit.show_effect.removed) then
                    unit.show_effect = unit:add_effect([[GN_gthts.mdl]])
                end
            else
                if unit.show_effect then
                    unit.show_effect:remove()
                end
            end
        end))
    end

    function mt:odds(player)
        local shop = self.owner
        return 30 + shop.owner:get_data('第一页抽取次数',0) * 10
    end
    function mt:get_food(player)
        local shop = self.owner
        return 50 + shop.owner:get_data('第一页数量',0) * 10
    end

    function mt:on_sell(unit,player)
        local shop = self.owner
        local odds = self:odds()
        player:set_data('引导-杀敌兑换',true)
        player:notify('引导-显示内容',player)
        if odd_run(odds) then
            local show_num = player:get_data('第一页数量',0) + 1
            self:set_enable(false)
            load_item_state(unit,self,1)
            player:set_data('第一页数量',show_num)
            player:set_data('第一页抽取次数',nil)
            if show_num==#list1 then
                shop:remove_skills()
                shop:remove()
                player:notify('玩家-创建神秘商店',player)
                if unit.show_effect then
                    unit.show_effect:remove()
                end
                -- ac.wait(1000,function()
                --     for _,name in ipairs(list2) do
                --         shop:add_skill(name,'英雄')
                --     end
                -- end)
            end
            --show_skill_tip(player,self.slotid,'兑换成功',0,-30)
            show_skill_tip(player,self.slotid,'刷新')
        else
            player:set_data('第一页抽取次数',player:get_data('第一页抽取次数',0) + 1)
            show_skill_tip(player,self.slotid,'概率提升')
        end
    end
end

for _,name in ipairs(list2) do
    local mt = ac.skill[name]

    function mt:get_food()
        local sd = self.xd + (self.ex or 0) * self.show_num
        return sd
    end

    function mt:on_sell(unit,player)
        local hero = player.hero
        if self.max==nil or self.show_num<self.max then
            self.show_num = self.show_num + 1
            hero:notify('单位-杀敌兑换成功',hero,self)
            show_skill_tip(player,self,'刷新')
            load_item_state(hero,self,1)
            if self.title=='六级装备吞噬丹' then
                collate_item(hero,'六级吞噬丹')
            end
            if self.title=='觉醒神石' then
                hero:create_jx_item()
            end
            if self.title=='杀敌抽奖' then
                hero:notify('单位-杀敌抽奖',unit)
            end
        end
        local count = player:get_data('解锁神秘商店计数',0) + 1
        player:set_data('解锁神秘商店计数',count)
        if count>=20 then
            player:notify('玩家-创建神秘商店',player)
        end
    end
end


local sx = {}
local zb = {}
for key,v in pairs(load_excel('特殊奖池.xlsx','杀敌抽奖')) do
    local data1 = v['参数1']
    local data2 = v['参数2']
    local data3 = v['参数3']
    local data4 = v['参数4']
    local data5 = v['参数5']
    if key:find('内容') then
        sx[#sx + 1] = {data1,data2,data3 or data3,data4,data5 or data4}
    else
        zb[#zb + 1] = {data1,data2,data3,data4 or 0}
    end
end

ac.game:event '单位-杀敌抽奖'(function(_,unit)
    local player = unit.owner
    local hero = player.hero
    local count = hero:get_data('杀敌抽奖次数',0) + 1
    local lv = math.min(math.floor(count/10),5)
    hero:set_data('杀敌抽奖次数',count)
    
    local data = table.random(sx)
    if data[1]~='装备池' then
        local key = data[1]
        local min = data[2]
        local max = data[3]
        local ad1 = data[4] or 0
        local ad2 = data[5] or ad1
        local num = math.random(min + ad1 * lv, max + ad2 * lv)
        hero:add(key,num)
        player:send_msg(('[抽奖]%s+%s'):format(key,num))
        return
    end

    
    local weight = {}
    for _,da in ipairs(zb) do
        if lv>=da[1] then
            weight[#weight + 1] = {da[2],da[3] + da[4]*lv}
        end
    end

    local lv = table.get_by_weight(weight)
    local name = random_item_name(lv)
    local item = hero_add_item(hero,name)
    player:send_msg(('[抽奖]获得:%s'):format(item:get_title()))
end)

ac.sync:event '同步-十连购买'(function(_,player,handle,ability_id)
    local unit = ac.unit(handle)
    if unit and unit.owner==player then
        local skill = unit.play_skills and unit.play_skills[ability_id]
        if skill then
            for a=1,10 do
                skill:cast()
            end
        end
    end
end)


ac.console:event '控制台-右击技能按钮'(function(_,ability_id)
    local unit = ac.unit.localunit()
    ac.sync('同步-十连购买',unit.handle,ability_id)
end)